<?php
// +----------------------------------------------------------------------
// | 十分彩网系统 [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace Wechat\Controller;
use Common\Controller\HomebaseController;
/**
 * 首页
 */
class MenuController extends HomebaseController {
	public function index(){
		vendor('Wechat.WxAdv');
		$date = date("Y-m-d H:i:s");
		$where['use_time']=array('gt',$date);
		$where['status']=array('eq',"1");
		$where['id']=array('eq',"1");
		$token = M('token')->field("access_token,use_time")->where($where)->find();
		if(!empty($token) && is_array($token)){
			$weixin = new \weixin_adv(C('APPID'), C('APPSECRET'),$token);
		}else{
			$weixin = new \weixin_adv(C('APPID'), C('APPSECRET'));
			$arr = $weixin->get_token();
			$arr['create_time'] = date("Y-m-d H:i:s");
			$arr['status']="1";
			M('token')->where(array("id"=>"1"))->save($arr);
		}
		$data = M('wxmenu')->where(array("status"=>'1',"level"=>"0"))->order("series asc")->select();
		if(!empty($data) && is_array($data)){
			foreach ($data as $key=>$item){
				$sub = M('wxmenu')->where(array("status"=>'1',"level"=>$item['id']))->order("series asc")->select();
				if(!empty($sub) && is_array($sub)){
					$result["button"][$key]["name"] = $item['name'];
					foreach ($sub as $k=>$v){
						$result["button"][$key]["sub_button"][$k]["type"] = $v['type'];
						$result["button"][$key]["sub_button"][$k]["name"] = $v['name'];
						if(trim($v['type']," ")==="view"){
							$result["button"][$key]["sub_button"][$k]["url"] = $v['url'];
						}
						if(trim($v['type']," ")==="click"){
							$result["button"][$key]["sub_button"][$k]["key"] = $v['key'];
						}
					}
				}else{
					$result["button"][$key]["type"] = $item['type'];
					$result["button"][$key]["name"] = $item['name'];
					if($item['type']=="view"){
						$result["button"][$key]["url"] = $item['url'];
					}elseif($item['type']=="click"){
						$result["button"][$key]["key"] = $item['key'];
					}
					
				}
			}
		}
		$json = json_encode($result,JSON_UNESCAPED_UNICODE);
		$wx = $weixin->create_menu($json);
		if($wx['errmsg'] == "ok"){
			return true;
		}else{
			return false;
		}
	}
	public function checknature(){
		if(!empty($_GET['data'])){
			$jsonArr = json_decode($_GET['data']);
			p($jsonArr);
		}
		//echo $_GET;
	}
}
?>